<?php

namespace app\admin\controller;

use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Storework extends AdminControl
{

    const EXPORT_SIZE = 1000;

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/Storework.lang.php');
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/zhuwork.lang.php');
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/admin.lang.php');
    }

    /**
     * 运营商客服管理
     */
    public function index()
    {
        $model = model('storework');
        $condition = [];
        /**
         * 检索条件
         */
        if (!empty(input('param.search_name'))) {
            $condition['name'] = array('like', "%" . input('param.search_name') . "%");
        }
        if (!empty(input('param.search_truename'))) {
            $condition['truename'] = array('like', "%" . input('param.search_truename') . "%");
        }
        if (!empty(input('param.search_yunyingshang'))) {
            $condition['s.store_name'] = array('like', "%" . input('param.search_yunyingshang') . "%");
        }
        if (!empty(input('param.search_admin_name'))) {
            $condition['admin_name'] = array('like', "%" . input('param.search_admin_name') . "%");
        }
        $list = $model->getList($condition, "*", 10);
        $this->assign('showpage', $model->page_info->render());
        $this->assign('list', $list);
        $this->assign('search_name', trim(input('param.search_name')));
        $this->assign('search_truename', trim(input('param.search_truename')));
        $this->assign('search_yunyingshang', trim(input('param.search_yunyingshang')));
        $this->assign('search_admin_name', trim(input('param.search_admin_name')));
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 增加运营商客服
     */
    public function add()
    {
        $model = model('storework');
        if (request()->isPost()) {
            // $validate = validate('storework');
            // if (!$validate->scene('add')->check($_POST)) {
            //     $this->error($validate->getError());
            // }
            #生成极光账号
            $post = [
                'username' => $_POST['account'],
                'password' => $_POST['password'],
                'nickname' => $_POST['name'],
                'avatar' => '',
                'signature' => ''
            ];
            $J_name = JmRegist($post);
            $J_info = json_decode($J_name, true);
            
            unset($_POST['queren_password']);
            $_POST['password'] = md5($_POST['password']);
            $_POST['login_num'] = 0;
            $_POST['admin_id'] = session('admin_id');
            $_POST['admin_name'] = session('admin_name');
            $_POST['created_at'] = $_POST['updated_at'] = date('Y-m-d H:i:s', time());
            $_POST['store_j_name'] = $J_info[0]['username'];
            
            $store_id = $_POST['storeid'];

            //保存到member表，登录运营商可以看到
            $store_info = db('store')->where(['store_id'=>$store_id])->find();
            db('member')->where(['member_id'=>$store_info['member_id']])->update(['customer_j_name'=>$_POST['store_j_name']]);

            if ($id = $model->add($_POST)) {
                //将客服id保存到对应的运营商上面
                db('store')->where(['store_id'=>$store_id])->update(['store_j_name'=>$id]);

                $this->log(lang('ds_add') . lang('storework_index_zhuwork') . '[' . input('post.name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            } else {
                $this->error(lang('ds_common_save_fail'));
            }
        } else {
            $store_model = model('store');
            $condition['store_seller_type'] = 1;
            $storeArr = $store_model->getAllStore($condition, 'store_id,store_name');
            $arr = [
                'id' => '',
                'storeid' => '',
                'name' => '',
                'account' => '',
                'password' => '',
                'status' => 1,
                'truename' => '',
                'idcard' => '',
                'sex' => 0,
                'status2' => 1
            ];
            $this->assign('array', $arr);
            $this->assign('storeArr', $storeArr);
            return $this->fetch('form');
        }
    }

    /**
     * 修改运营商客服
     */
    public function edit()
    {
        $model = model('storework');
        if (request()->isPost()) {
            // $validate = validate('storework');
            // if (!$validate->scene('edit')->check($_POST)) {
            //     $this->error($validate->getError());
            // }
            $_POST['updated_at'] = date('Y-m-d H:i:s', time());
            if (strlen($_POST['password'])) {
                $_POST['password'] = md5($_POST['password']);
            } else {
                unset($_POST['password']);
            }
            unset($_POST['queren_password']);
            $res = $model->edit(['id' => input('post.id')], $_POST);
            if ($res) {
                $this->log(lang('ds_update') . lang('storework_index_zhuwork') . '[' . input('post.name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            } else {
                $this->log(lang('ds_update') . lang('storework_index_zhuwork') . '[' . input('post.name') . ']', 0);
                $this->error(lang('ds_common_save_fail'));
            }
        } else {
            $store_model = model('store');
            $condition['store_seller_type'] = 1;
            $storeArr = $store_model->getAllStore($condition, 'store_id,store_name');
            $res = $model->getInfo(['id' => input('get.storeworkid')]);
            $this->assign('array', $res);
            $this->assign('storeArr', $storeArr);
            return $this->fetch('edit');
        }
    }

    /**
     * 运营商状态修改
     */
    public function edit_status()
    {
        $model = model('storework');
        if (request()->isPost()) {
            $condition['id'] = input('post.id');
            $data['status'] = input('post.status');
            if ($data['status']  == 2) {
                $data['fail_reason'] = input('post.fail_reason');
            }
            $res = $model->edit($condition, $data);
            if ($res) {
                $this->log(lang('ds_update') . lang('storework_index_zhuwork') . '[ID:' . intval(input('post.name')) . ')', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            } else {
                $this->log(lang('ds_update') . lang('storework_index_zhuwork') . '[' . input('post.name') . ']', 0);
                $this->error(lang('ds_common_save_fail'));
            }
        } else {
            $array = $model->getInfo(['id' => input('param.storeworkid')]);
            $this->assign('array', $array);
            return $this->fetch('edit_status');
        }
    }

    public function edit_status2()
    {
        $zhuwork_model = model('storework');
        $condition['id'] = input('get.id');
        $data[input('get.column')] = input('get.value');
        $info = $zhuwork_model->edit($condition, $data);
        $this->log(lang('ds_edit') . lang('zhuwork_index_zhuwork') . '[ID:' . intval(input('get.id')) . ')', 1);
        ds_json_encode(10000, lang('ds_common_op_succ'));
    }

    /**
     * 删除运营商客服
     */
    public function del()
    {
        $model = model('storework');
        $storeworkid = input('param.storeworkid');
        $storeworkidArr = ds_delete_param($storeworkid);
        if ($storeworkidArr == FALSE) {
            $this->log(lang('ds_del') . lang('storework_index_zhuwork') . '[ID:' . $storeworkid . ']', 0);
            ds_json_encode(10001, lang('param_error'));
        }
        $model->del(array('id' => array('in', implode(',', $storeworkidArr))));
        $this->log(lang('ds_del') . lang('storework_index_zhuwork') . '[ID:' . $storeworkid . ']', 1);
        ds_json_encode(10000, lang('ds_common_del_succ'));
    }

    /**
     * ajax
     */
    public function ajax()
    {
        $model = model('storework');
        switch (input('get.branch')) {
            case 'check_work_name':
                $condition['name'] = input('get.workname');
                $condition['id'] = ['neq', input('get.workid')];
                $info = $model->getInfo($condition);
                if ($info) {
                    exit('false');
                } else {
                    exit('true');
                }
            case 'check_work_account':
                $condition['account'] = input('get.workaccount');
                $condition['id'] = ['neq', input('get.workid')];
                $info = $model->getInfo($condition);
                if ($info) {
                    exit('false');
                } else {
                    exit('true');
                }
                //            case 'edit_status':
                //                $condition['id']= input('param.id');
                //                $data[input('param.column')] = input('param.value');
                //                $model->editWorktype($condition,$data);
                //                $this->log(lang('ds_edit') . lang('worktype_index_zhuwork') . '[ID:' . intval(input('param.id')) . ')', 1);
                //                echo  'true';
                //                exit;
                //                break;
        }
    }


    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_manage'),
                'url' => url('Storework/index'),
            ),
            array(
                'name' => 'add',
                'text' => lang('ds_add'),
                'url' => "javascript:dsLayerOpen('" . url('Storework/add') . "','" . lang('ds_add') . "')"
            ),
        );
        return $menu_array;
    }
}
